﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataGrid全选
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<string> selectIds = new List<string>();
        public MainWindow()
        {
            InitializeComponent();

            InitData();
        }
        /// <summary>
        /// 初始化一些数据
        /// </summary>
        public void InitData()
        {
            var users = new List<Person>();
            for (int i = 0; i < 10; i++)
            {
                var user = new Person();
                user.Id =i.ToString();
                user.Name = "李"+i.ToString();
                user.Age =10+i;
                user.IsSelected=false;
                users.Add(user);
            }
            dg_Contrast.ItemsSource= users;
        }

        private void Contrast_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var currentPage = 1;
            e.Row.Header = (currentPage-1)*10 + e.Row.GetIndex() + 1;
        }
        /// <summary>
        /// 全选/全不选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cb_SelectAll_Click(object sender, RoutedEventArgs e)
        {
            CheckBox dg = sender as CheckBox;
            var isChecked = dg.IsChecked;
            var users = dg_Contrast.ItemsSource as List<Person>;
            if (isChecked==true)
            {
                //全选
                foreach (var user in users)
                {
                    if (!selectIds.Contains(user.Id))
                    {
                        selectIds.Add(user.Id);
                    }
                }
            }
            else
            {
                //取消全选
                foreach (var user in users)
                {
                    if (selectIds.Contains(user.Id))
                    {
                        selectIds.Remove(user.Id);
                    }
                }
            }
            UpdateDataGrid();
        }
        /// <summary>
        /// 单个选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox dg = sender as CheckBox;
            //获取该行的Id
            string id = dg.Tag.ToString();
            var isChecked = dg.IsChecked;
            if (isChecked == true)
            {
                if (!selectIds.Contains(id))
                {
                    //如果选中就保存Id
                    selectIds.Add(id);
                }
            }
            else
            {
                //如果取消选中就删除里面的Id
                selectIds.Remove(id);
            }
            UpdateDataGrid();
        }
        public void UpdateDataGrid()
        {
            var newSource = dg_Contrast.ItemsSource as List<Person>;
            if (newSource!=null)
            {
                foreach (var user in newSource)
                {
                    if (selectIds.Contains(user.Id))
                    {
                        user.IsSelected=true;
                    }
                    else
                    {
                        user.IsSelected=false;
                    }
                }
                //全选按钮
                if (newSource.Count()==selectIds.Count())
                {
                    cb_SelectAll.IsChecked=true;
                }
                else
                {
                    cb_SelectAll.IsChecked=false;
                }
            }
        }

        private void dg_Contrast_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var users = dg_Contrast.SelectedItems;
            foreach (var item in users)
            {
                var user = item as Person;
                if (!selectIds.Contains(user.Id))
                {
                    //如果选中就保存Id
                    selectIds.Add(user.Id);
                }
                else
                {
                    //如果已存在则删除
                    selectIds.Remove(user.Id);
                }
            }
            UpdateDataGrid();
        }
    }
}
